home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / rexx / mrnbst.ime / MRNBSTime.c < prev    next >
C/C++ Source or Header  |  1990-03-04  |  4KB  |  124 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <time.h>
  6. #include <exec/types.h>
  7. #include <libraries/dosextens.h>
  8.  
  9. static int  tz_hour_diff =      0;
  10. static int  tz_minutes_diff =   0;
  11.  
  12. static char *monthNames[12] = {
  13.     "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Nov","Dec"
  14.     };
  15.  
  16. int
  17. ConvertUTC(const char *s, int hourDiff, int minuteDiff, char *dosDate)
  18. {
  19. #define    EPOCH        40587                    /* UNIX starts JD 2440587, */
  20. #define    TIME        "%05ld %03d %02d%02d%02d UTC"
  21.     int         result = 0;
  22.     time_t      now;
  23.     long        julian;
  24.     int            day_of_year;
  25.     int            hour;
  26.     int            min;
  27.     int            sec;
  28.     struct tm   *lt;
  29.  
  30.     if(sscanf(s,TIME, &julian, &day_of_year, &hour, &min, &sec) != 5)
  31.     {
  32.         fprintf(stderr,"Garbled time code: %s\n", s);
  33.         result = 1;
  34.     }
  35.     else {
  36.         now = (((julian - EPOCH) * 24 + hour) * 60 + min) * 60 + sec;
  37.         now += ((minuteDiff * 60) + (hourDiff * 3600));
  38.         lt = localtime(&now);
  39.         sprintf(dosDate,"%d-%s-%d %02d:%02d:%02d",
  40.                 lt->tm_mday,
  41.                 monthNames[lt->tm_mon],
  42.                 lt->tm_year,
  43.                 lt->tm_hour,
  44.                 lt->tm_min,
  45.                 lt->tm_sec);
  46.         fprintf(stderr,"GMT time: %s\n", dosDate);
  47.     }
  48.     return result;
  49. }
  50.  
  51. main(argc, argv)
  52.     int argc;
  53.     char **argv;
  54. {
  55. #define UTC_LENGTH      20      /* length of UTC string */
  56.     int     argn = 0;
  57.     int     gotit = 0;
  58.     int     leng;
  59.     int     returnValue = 0;
  60.     char    lastString[81];
  61.     char    timeString[81];
  62.  
  63.     while (++argn < argc) {
  64.         if (strcmp(argv[argn], "-h") == 0) {
  65.             tz_hour_diff = atoi(argv[++argn]);
  66.             if (tz_hour_diff < -12 || tz_hour_diff > 12) {
  67.                 fprintf(stderr,"%s: bad delta hours value!\n", argv[0]);
  68.                 return 1;
  69.             }
  70.         }
  71.         else if (strcmp(argv[argn], "-m") == 0) {
  72.             tz_minutes_diff = atoi(argv[++argn]);
  73.             if (tz_minutes_diff < -59 || tz_minutes_diff > 59) {
  74.                 fprintf(stderr,"%s: bad delta minutes value!\n", argv[0]);
  75.                 return 1;
  76.             }
  77.         }
  78.         else if (*argv[argn] == '-') {
  79. usage:
  80.             fprintf(stderr,
  81.                     "Usage: %s [-h <hours>] [-m <minutes>] [ <filename> ]\n", argv[0]);
  82.             fprintf(stderr,
  83.                     "<hours> (+-12) and <minutes> (+-59) are your time delta from GMT.\n");
  84.             fprintf(stderr,
  85.                     "If <filename> is not given, standard input is assumed.\n");
  86.         }
  87.         else {
  88.             if ( (argc - argn) > 1) goto usage;
  89.             if (! freopen(argv[argn], "r", stdin) ) {
  90.                 fprintf(stderr,"%s: %s would not open for input!\n",
  91.                         argv[0], argv[argn]);
  92.                 return 1;
  93.             }
  94.             break;
  95.         }
  96.     }
  97.     /* Scan standard input (normally a pipe or file), looking for valid universal
  98.      * time code strings. Save the last one for conversion. 
  99.      */
  100.     while (gets(timeString)) {
  101.         leng = strlen(timeString);
  102.         if (leng >= UTC_LENGTH) {
  103.             if (strcmp(timeString+leng-3, "UTC") == 0) {
  104.                 gotit = 1;
  105.                 strcpy(lastString, timeString);
  106.                 break;
  107.             }
  108.         }
  109.     }
  110.     if (gotit) {
  111.         if ((returnValue = 
  112.              ConvertUTC(lastString, tz_hour_diff, tz_minutes_diff, timeString)) == 0) {
  113.             fprintf(stderr, "%s\n", timeString);
  114.             fexecl("Date","Date",timeString,NULL);
  115.             returnValue = wait();
  116.         }
  117.     }
  118.     else {
  119.         fprintf(stderr,"No UTC strings found in input file!\n");
  120.         returnValue =  1;
  121.     }
  122.     return returnValue;
  123. }
  124.